package gui.mainView;

import gui.advancedView.UIAdvancedView;
import gui.commons.ButtonColumn;
import gui.commons.Resources;
import gui.commons.RoundedPanel;
import gui.passwordView.UIPasswordView;

import java.awt.Color; 
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JTable;

import commons.Manager;
import commons.Media;
import commons.User;
import commons.exceptions.PasswordException;
import commons.exceptions.ProcessException;

public class UIView extends JFrame{


	private static final long serialVersionUID = 1L;
	
	public static final int Update_UserPan = 1;
	public static final int Update_InfoPan = 2;
	public static final int Update_MediaPan = 3;
	public static final int Update_GuestPan = 4;
	public static final int Update_SouthPan = 5;
	public static final int Update_InfoAndMediaPan = 6;
	public static final int Update_InfoAndSouthPan = 7;
	public static final int Update_InfoMediaAndSouthPan = 8;
	
	private static UIView INSTANCE = null;
	
	private Manager parent;
	private UIControler controler;
	
	public JButton play;
	public JButton next;
	public JButton controls;
	public JButton stop;
	public JButton parameters;

	private JLayeredPane container;
	private JLayeredPane infoPan = new JLayeredPane();//Meta donnees
	//private JPanel sousPan = new RoundedPanel();
	private JPanel userPan = new RoundedPanel();
	private JPanel mediaPan = new RoundedPanel();
	private JPanel guestPan = new RoundedPanel();
	private JPanel IPPan = new RoundedPanel();
	private JPanel south = new RoundedPanel();

	private JLabel artist;
	private JLabel album;
	private JLabel title;
	
	private JLabel guestPasswd;

	private Media media;

	private int x;
	private int y;
	
	public boolean settingsOpened=false;
	public boolean advancedOpened=false;


	private UIView(Manager parent){
		this.parent=parent;
		this.controler = new UIControler(this, parent);

		Resources.init();

		play = new JButton(new ImageIcon(Resources.playIcon));
		next = new JButton(new ImageIcon(Resources.nextIcon));
		controls = new JButton(new ImageIcon(Resources.controlIcon));
		stop = new JButton(new ImageIcon(Resources.stopIcon));
		parameters = new JButton(new ImageIcon(Resources.paramIcon));

		Dimension dimension = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
		y = (int)dimension.getHeight();
		x = (int)dimension.getWidth();

		container = new JLayeredPane(){
			private static final long serialVersionUID = 1;
			Dimension dim = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
			@Override
			protected void paintComponent(Graphics g)
			{
				super.paintComponent(g);
				g.drawImage(Resources.background, 0,0, null);
			}
		};
		
		play.addActionListener(controler);
		play.setBorder(null);
		play.setBackground(null);

		next.addActionListener(controler);
		next.setBorder(null);
		next.setBackground(null);

		controls.addActionListener(controler);
		controls.setBorder(null);
		controls.setBackground(null);
		
		stop.addActionListener(controler);
		stop.setBorder(null);
		stop.setBackground(null);

		parameters.addActionListener(controler);
		parameters.setBorder(null);
		parameters.setBackground(null);
		
		userPan.setBackground(Color.BLACK);
		userPan.setForeground(Color.WHITE);
		userPan.setBounds((int) (x*0.01), (int) (y*0.05), (int) (x*0.22), (int) (y*0.72));

		mediaPan.setBackground(Color.BLACK);
		mediaPan.setForeground(Color.WHITE);
		mediaPan.setBounds((int) (x*0.72), (int) (y*0.05), (int) (x*0.27), (int) (y*0.72));

		infoPan.setForeground(Color.WHITE);
		infoPan.setBackground(Color.BLACK);
		infoPan.setBounds((int) (x*0.25), (int) (y*0.57), (int) (x*0.44), (int) (y*0.20));
		infoPan.setBorder(BorderFactory.createLineBorder(Color.white));

		south.setBackground(Color.BLACK);
		south.setForeground(Color.WHITE);
		south.add(play);
		south.add(next);
		south.add(stop);
		south.add(controls);
		south.setBounds((int)(x*0.25), (int) (y*0.81), (int) (x*0.44), (int) (y*0.11));
		//----------------
		IPPan.setBackground(Color.BLACK);
		IPPan.setForeground(Color.WHITE);
		IPPan.setBounds((int)(x*0.72), (int) (y*0.81), (int) (x*0.27), (int) (y*0.11));
		
		Font font = new Font("Arial", Font.PLAIN, (int)(x/55));
		String ip=parent.getIpAddress();
		JLabel iptext;
		if(ip.equals("")){
			iptext = new JLabel("No IP found");
		}
		else{
			iptext = new JLabel(ip+":8080/airpi/");
		}
		iptext.setFont(font);
		iptext.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		iptext.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
		iptext.setForeground(Color.white);
		
		IPPan.setLayout(new GridLayout());
		IPPan.add(iptext);
		
		guestPan.setBackground(Color.BLACK);
		guestPan.setForeground(Color.WHITE);
		guestPan.setBounds((int)(x*0.01), (int) (y*0.81), (int) (x*0.22), (int) (y*0.11));
				
		guestPan.setLayout(new GridBagLayout());
		
		Font f = new Font("Arial", Font.BOLD, (int)(x/100));
		JLabel textguest =new JLabel("guests' password:");
		textguest.setFont(f);
		textguest.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		textguest.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
		textguest.setForeground(Color.white);
		
		Font f11 = new Font("Arial", Font.PLAIN, (int)(x/30));
		try {
			this.guestPasswd=new JLabel(User.getGuestPassword());
			this.guestPasswd.setFont(f11);
			this.guestPasswd.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
			this.guestPasswd.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
			this.guestPasswd.setForeground(Color.white);
		} catch (PasswordException e2) {
		}

		Insets i = new Insets(0, -20, 0, 70); // Haut gauche Bas Droite
		GridBagConstraints gbc = this.createConstraint(0,0,1,2,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		guestPan.add(parameters,gbc);
		i = new Insets(0, -40, 0, 0);
		gbc = this.createConstraint(1,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		guestPan.add(textguest,gbc);
		gbc = this.createConstraint(1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		guestPan.add(this.guestPasswd,gbc);
				
		container.add(guestPan);
		container.add(infoPan);
		container.add(south);
		container.add(mediaPan);
		container.add(userPan);
		container.add(IPPan);

		this.setUndecorated(true);
		this.setTitle("AirPi");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setSize(x, y);
		this.setContentPane(container);
		this.setResizable(false);
		this.setVisible(true);
		
	}
	
	public static synchronized UIView getInstance(Manager manager)
	{	
		if (INSTANCE == null){ 
			INSTANCE = new UIView(manager);
		}
		return INSTANCE;
	}

	public void updateInfoPan(){
		if(parent.isPlaying()){
			this.album = new JLabel("   Album : " + media.getAlbum());
			this.album.setForeground(Color.white);
			this.artist = new JLabel("  Artist : "+media.getArtist());
			this.artist.setForeground(Color.white);
			this.title = new JLabel(" Title : "+media.getTitle());
			this.title.setForeground(Color.white);
			Font f1 = new Font("Arial", Font.BOLD, (int)(x/50)); // par exemple 
			this.title.setFont(f1); 
			Font f2 = new Font("Arial", Font.PLAIN, (int)(x/64)); // par exemple 
			this.artist.setFont(f2); 
			Font f3 = new Font("Arial", Font.BOLD, (int)(x/80)); // par exemple 
			this.album.setFont(f3); 
		}else if(parent.getMediaPlaying()!=null){
			this.album = new JLabel("   Album : " + media.getAlbum());
			this.album.setForeground(Color.white);
			this.artist = new JLabel("  Artist : "+media.getArtist());
			this.artist.setForeground(Color.white);
			this.title = new JLabel(" Title : "+media.getTitle());
			this.title.setForeground(Color.white);
			Font f1 = new Font("Arial", Font.BOLD, (int)(x/50)); // par exemple 
			this.title.setFont(f1); 
			Font f2 = new Font("Arial", Font.PLAIN, (int)(x/64)); // par exemple 
			this.artist.setFont(f2); 
			Font f3 = new Font("Arial", Font.BOLD, (int)(x/80)); // par exemple 
			this.album.setFont(f3); 
		}else{
			this.album = new JLabel("   Album : - ");
			this.album.setForeground(Color.white);
			this.artist = new JLabel("  Artist : -");
			this.artist.setForeground(Color.white);
			this.title = new JLabel(" Title : - ");
			this.title.setForeground(Color.white);
			Font f1 = new Font("Arial", Font.BOLD, (int)(x/50)); // par exemple 
			this.title.setFont(f1); 
			Font f2 = new Font("Arial", Font.PLAIN, (int)(x/64)); // par exemple 
			this.artist.setFont(f2); 
			Font f3 = new Font("Arial", Font.BOLD, (int)(x/80)); // par exemple 
			this.album.setFont(f3);
		}
		
		infoPan.removeAll();
		infoPan.setLayout(new GridLayout(3,1));
		infoPan.add(this.title);
		infoPan.add(this.artist);
		infoPan.add(this.album);
		infoPan.repaint();
		
	}
	
	public void updateMediaPan(){
		JTable tab = parent.getMediaList().getMediaListJTab();
		tab.getColumnModel().getColumn(0).setPreferredWidth((int) (x*0.16));
		tab.getColumnModel().getColumn(1).setPreferredWidth((int) (x*0.06));
		tab.getColumnModel().getColumn(2).setPreferredWidth((int) (x*0.04));
		tab.setShowGrid(false);
		tab.setIntercellSpacing(new Dimension(0,0));
		for(int i = 0;i<tab.getRowCount();i++){
			tab.setRowHeight(i, (int) (y*0.05));
		}
		Action delete = new AbstractAction()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		        int modelRow = Integer.valueOf(e.getActionCommand());
		        try {
					parent.delMedia(parent.getMediaList().getMediaAtIndex(modelRow));
				} catch (ProcessException e1) {
					System.out.println("Del media error");
				}
		    }
		};
		new ButtonColumn(tab,delete,2);
		this.mediaPan.removeAll();
		this.mediaPan.add(tab);
		this.mediaPan.repaint();
	}
	
	public void updateUserPan(){
		JTable tab = parent.getUserList().getUserListJTab();
		tab.getColumnModel().getColumn(0).setPreferredWidth((int) (x*0.11));
		tab.setShowGrid(false);
		tab.setIntercellSpacing(new Dimension(0, 0));
		for(int i = 0;i<tab.getRowCount();i++){
			tab.setRowHeight(i, (int) (y*0.05));
		}
		
		this.userPan.removeAll();
		this.userPan.add(tab);
		this.userPan.repaint();
	}
	
	public void updateGuestPan(){
		try {
			this.guestPasswd.setText(User.getGuestPassword());
		} catch (PasswordException e2) {}
		
		Font f1 = new Font("Arial", Font.BOLD, (int)(x/100));
		JLabel textguest =new JLabel("guests' password:");
		textguest.setFont(f1);
		textguest.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		textguest.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
		textguest.setForeground(Color.white);
		
		Font f2 = new Font("Arial", Font.PLAIN, (int)(x/30));
		this.guestPasswd.setFont(f2);
		this.guestPasswd.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
		this.guestPasswd.setVerticalAlignment(javax.swing.SwingConstants.CENTER);
		this.guestPasswd.setForeground(Color.white);
				
		this.guestPan.removeAll();
		Insets i = new Insets(0, -20, 0, 70); // Haut gauche Bas Droite
		GridBagConstraints gbc = this.createConstraint(0,0,1,2,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(parameters,gbc);
		i = new Insets(0, -40, 0, 0);
		gbc = this.createConstraint(1,0,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(textguest,gbc);
		gbc = this.createConstraint(1,1,1,1,GridBagConstraints.CENTER,GridBagConstraints.NONE,i);
		this.guestPan.add(this.guestPasswd,gbc);
		this.guestPan.repaint();
	}
	
	public void updateSouthPan(){
		if(parent.isPlaying()){
			this.play.setIcon(new ImageIcon(Resources.pauseIcon));
			this.play.setBorder(null);
			this.play.setBackground(null);
		}else if(parent.getMediaPlaying()!=null){
			this.play.setIcon(new ImageIcon(Resources.playIcon));
			this.play.setBorder(null);
			this.play.setBackground(null);
		}else{
			this.play.setIcon(new ImageIcon(Resources.playIcon));
			this.play.setBorder(null);
			this.play.setBackground(null);
		}
	}
	@SuppressWarnings("serial")
	public void update(int i){
		System.out.println("UIView updated");
		
		this.media = parent.getMediaPlaying();
		
		switch (i){

		case (Update_UserPan) :
			this.updateUserPan();
			break;
		case (Update_InfoPan) :
			this.updateInfoPan();
			break;
		case (Update_MediaPan) :
			this.updateMediaPan();
			break;		
		case (Update_GuestPan) :
			this.updateGuestPan();
			break;
		case (Update_SouthPan) :
			this.updateSouthPan();
			break;
		case (Update_InfoAndMediaPan) :
			this.updateInfoPan();
			this.updateMediaPan();
			break;
		case (Update_InfoAndSouthPan) :
			this.updateInfoPan();
			this.updateSouthPan();
			break;
		case (Update_InfoMediaAndSouthPan) :
			this.updateSouthPan();
			this.updateInfoPan();
			this.updateMediaPan();
			break;
			
		}
		this.validate();
	}
	
	public String getPlayerBounds(){
		return (x*0.23)+" "+(y*0.19)+" "+(x*0.71)+" "+(y*0.49);
	}

	public Dimension getDimension(){
		return new Dimension(x,y);
	}

	public GridBagConstraints createConstraint(int gx, int gy, int gw, int gh, int anchor, int fill, Insets insets){
		GridBagConstraints gbc=new GridBagConstraints();
		gbc.gridx=gx;	//colonne
		gbc.gridy=gy;	//ligne
		gbc.gridwidth = gw;	//nb de colonnes occupées
		gbc.gridheight = gh;	//nb de lignes occupées
		gbc.anchor=anchor;	//position du composant dans la cellule
		gbc.fill = fill;	//redimensionnement du composant
		gbc.insets = insets;	//marges autour du composant 
		return gbc;
	}

	public void kill(){
		UIPasswordView.getInstance(this).kill();
		UIAdvancedView.getInstance(this).kill();
		this.dispose();
	}
}
